<!---
Copyright: (c) 2006 The MachBlog Authors
Authors: Matt Woodward (mpwoodward@gmail.com) & Peter J. Farrell (pjf@maestropublishing.com)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

http://www.apache.org/licenses/LICENSE-2.0 

Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License.

$Id: LoginListener.cfc 940 2006-08-24 01:09:59Z mpwoodward $

Notes:
This listener is autowired by ColdSpring.
--->
<cfcomponent
	displayname="LoginListener"
	output="false" 
	extends="MachII.framework.Listener"
	hint="LoginListener for MachBlog">

	<!---
	INITIALIZATION / CONFIGURATION
	--->
	<cffunction name="configure" access="public" returntype="void"  output="false"
		hint="Configures the listener as part of the Mach-II framework.">
		<!--- Does nothing --->
	</cffunction>

	<cffunction name="setLoginService" access="public" returntype="void" output="false"
		hint="Dependency: injected">
		<cfargument name="loginService" type="machblog.org.machblog.security.LoginService" required="true" />
		<cfset variables.loginService = arguments.loginService />
	</cffunction>	
	<cffunction name="getLoginService" access="private" returntype="machblog.org.machblog.security.LoginService" output="false">
		<cfreturn variables.loginService />
	</cffunction>
	
	<!---
	PUBLIC FUNCTIONS
	--->
	<cffunction name="processLoginAttempt" access="public" returntype="void" output="false"
			hint="Processes a login attempt and announces the next event.">
		<cfargument name="event" type="MachII.framework.Event" required="true" />
		
		<cfset var success = false />
		
		<cfset success = getLoginService().validateLogin(arguments.event.getArg("email"), arguments.event.getArg("password")) />
		
		<cfif success>
			<cfset announceEvent("loginSucceeded") />
		<cfelse>
			<cfset arguments.event.setArg("message", getProperty("resourceBundleService").getResourceBundle().getResource("loginfailedmessage")) />
			<cfset announceEvent("loginFailed", arguments.event.getArgs()) />
		</cfif>
	</cffunction>
	
	<cffunction name="processForgotPasswordForm" access="public" output="false" returntype="void" 
			hint="Generates a new password and emails it to the user">
		<cfargument name="event" type="MachII.framework.Event" required="true" />
		
		<cfset var userId = "" />
		
		<cfset userId = getLoginService().getUserIdByEmailAddress(arguments.event.getArg("email")) />
		
		<cfif userId EQ "">
			<cfset arguments.event.setArg("message", 
							getProperty("resourceBundleService").getResourceBundle().getResource("emaildoesntexist")) />
		<cfelse>
			<cftry>
				<cfset getLoginService().resetPassword(userId, arguments.event.getArg("email"), getProperty("emailWebmaster"), 
														getProperty("blogHeaderTitle") & "-" & 
														getProperty("resourceBundleService").getResourceBundle().getResource("passwordreset"), 
														getProperty("resourceBundleService").getResourceBundle().getResource("passwordresetemailtext")) />
				<cfset arguments.event.setArg("message", getProperty("resourceBundleService").getResourceBundle().getResource("passwordresetmessage")) />
				<cfcatch type="any">
					<cfset arguments.event.setArg("message", getProperty("resourceBundleService").getResourceBundle().getResource("errormessagegeneric")) />
				</cfcatch>
			</cftry>
		</cfif>
		
		<cfset announceEvent("showForgotPassword", arguments.event.getArgs()) />
	</cffunction>
	
	<cffunction name="logout" access="public" returntype="void"  output="false"
			hint="Logs the user out of the application and announces the next event.">
		<cfargument name="event" type="MachII.framework.Event" required="true" />
		
		<cfset getLoginService().logout() />
		<cfset arguments.event.setArg("message", getProperty("resourceBundleService").getResourceBundle().getResource("logoutmessage")) />
		<cfset announceEvent("logoutSucceeded", arguments.event.getArgs()) />
	</cffunction>

</cfcomponent>